<?php
    /* Category Related Functions */
    function getAllCategories() {
        $sql = "SELECT * FROM categories";
        $result = mysql_query($sql);
                
        if (!$result){
            die("Tidak Dapat Mengeksekusi Query" . mysql_error());
        }
        else {
            return $result;
        }        
    }

    /* Comment Related Functions */
    function getComments ($post_id) {
        $sql = "SELECT * FROM comments WHERE post_id = " . $post_id;
        $result = mysql_query($sql);
                
        if (!$result){
            die("Tidak Dapat Mengeksekusi Query" . mysql_error());
        }
        else {
            return $result;
        }
    }
    
    function insertComment($name, $email, $comment, $post_id) {
        $sql = "INSERT INTO comments VALUES(NULL, '" . $name. "',  '" . $email. "',  '" . $comment. "',  " . $post_id . ");";
        $result = mysql_query ($sql);
        
        if (!$result){
            die("Tidak Dapat Mengeksekusi Query" . mysql_error());
        }
        else {
            return $result;
        }
    }
    
    /* Navigation Related Functions */
    function getAllMenus() {
        $sql = "SELECT * FROM menus";        
        $result = mysql_query ($sql);
        
        if (!$result) {
            die("Tidak Dapat Mengeksekusi Query" . mysql_error());
        }
        else {
            return $result;
        }
    }
    
    /* Post Related Functions */
    function getAllPostByAuthor($author_id) {
        $sql = "SELECT 
                    posts.id AS 'post_id',
                    title,
                    authors.name AS 'author_name' 
                FROM 
                    posts,
                    authors 
                WHERE 
                    posts.author_id = authors.id AND
                    posts.author_id = " . $author_id;
        
        $result = mysql_query ($sql);
        
        if (!$result){
            die("Tidak Dapat Mengeksekusi Query: " . mysql_error());
        }
        else {
            return $result;
        }
    }
    
    function getAllPosts() {
        $sql = "SELECT 
                    posts.id AS 'post_id',
                    title,
                    authors.name AS 'author_name' 
                FROM 
                    posts,
                    authors 
                WHERE 
                    posts.author_id = authors.id";
        
        $result = mysql_query ($sql);
        
        if (!$result){
            die("Tidak Dapat Mengeksekusi Query: " . mysql_error());
        }
        else {
            return $result;
        }
    }    
    
    function getPost($post_id) {
        $sql = "SELECT * FROM posts WHERE posts.id= " . $post_id;
        
        $result = mysql_query ($sql);
        
        if (!$result){
            die("Tidak Dapat Mengeksekusi Query: " . mysql_error());
        }
        else {
            return $result;
        }
    }
    
    function getRecentPosts($number_of_post) {
        $sql = "SELECT * FROM posts ORDER BY published_time DESC LIMIT " . $number_of_post;
        $result = mysql_query ($sql);
        
        if (!$result){
            die("Tidak Dapat Mengakses Query" . mysql_error());
        }
        else {
            return $result;
        }
    }
?>
